<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php
require_once('php_shared/session_to_db.php');

$info_msg = "";
$error_msg = "";

if( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
	if( isset($_POST['username'], $_POST['password1'], $_POST['secret_question'], $_POST['secret_answer']) )
	{
		$db_man = new DbUtils();
		
		if( $db_man->connect() )
		{
			$uname = $db_man->set_var($_POST['username']);
			$sql = "SELECT id FROM registered_user WHERE username=$uname";
			
			$result = $db_man->query($sql);
			if( !$result )
			{
				$error_msg = "There was a problem communicating with database, please try again later.";
			}else{
				if( mysql_num_rows($result) == 0 )
				{
					$pwd = $db_man->set_var($_POST['password1']);
					$sq = $db_man->set_var($_POST['secret_question']);
					$sa = $db_man->set_var($_POST['secret_answer']);
					$sql = "INSERT INTO registered_user (username, password, secret_question, secret_answer) VALUES ($uname, $pwd, $sq, $sa)";
					$result = $db_man->query($sql);
					if( !$result )
					{
						$error_msg = "There was a problem communicating with database, please try again later.";
					}else{
						$info_msg = "Registered successfuly, you can sign in using your new username from the login page.";
					}
				}else{
					$error_msg = "There already exists a user with the same username, please try a different one.";
				}
			}
			
			$db_man->close();
		}
	}
}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Register New User | CSE315 IM</title>
    <link href="css/shared.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="js/registration.js"></script>
</head>

<body>

	<div id="container">
        <b class="border1"></b><b class="border2"></b><b class="border3"></b><b class="border4"></b>
		<div class="bordercontent">
        
        	<img src="img/CSE315IM_logo.png" width="317" height="79" alt="CSE315 IM Logo" />
       		<br />
            <h3 class="page_title">Registration</h3>
            <br />
        	<form action="registration.php" method="post" name="registration_form" onSubmit="JavaScript:return check_reg_form();">
                <label>Username:</label><br />
                <input name="username" id="username" type="text" size="45" maxlength="20" /><br />
                <br />
                <label>Password:</label><br />
                <input name="password1" id="password1" type="password" size="45" maxlength="20" /><br />
                <br />
                <label>Confirm Password:</label><br />
                <input name="password2" id="password2" type="password" size="45" maxlength="20" /><br />
                <br />
                <label>Secret Question:</label><br />
                <input name="secret_question" id="secret_question" type="text" size="45" maxlength="40" /><br />
                <br />
                <label>Answer of Secret Question:</label><br />
                <input name="secret_answer" id="secret_answer" type="text" size="45" maxlength="40" /><br />
                <br />
                <input name="reg_button" type="submit" value="Register" class="form_submit"/>
			</form>
            <br /><br />
            <label class="info"><?php print $info_msg; ?></label><br />
            <label class="error"><?php print $error_msg; ?></label><br />
            <a href="login.php">Already registered?</a><br />
            
        </div><!-- end #bordercontent -->
	    <b class="border4"></b><b class="border3"></b><b class="border2"></b><b class="border1"></b>
    </div><!-- end #container -->
    
</body>
</html>